<!DOCTYPE html>




<html class="theme-next gemini" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Android测试,测试," />





  <link rel="alternate" href="/atom.xml" title="流水不腐小夏" type="application/atom+xml" />






<meta name="description" content="Android测试总结 [TOC] 简介最近整理了Android测试方便的只是，主要涉及代码测试和自动化测试。 代码测试JunitJUnit浅谈 MockitoMockito浅谈 MockwebserverMockWebServer Android自动化测试Android monkeyAndroid Monkey整理 Android monkeyrunnerAndroid monkeyrunner">
<meta name="keywords" content="Android测试,测试">
<meta property="og:type" content="article">
<meta property="og:title" content="Android测试总结">
<meta property="og:url" content="http://mycommons.cn/2016/07/25/Android-Test/index.html">
<meta property="og:site_name" content="流水不腐小夏">
<meta property="og:description" content="Android测试总结 [TOC] 简介最近整理了Android测试方便的只是，主要涉及代码测试和自动化测试。 代码测试JunitJUnit浅谈 MockitoMockito浅谈 MockwebserverMockWebServer Android自动化测试Android monkeyAndroid Monkey整理 Android monkeyrunnerAndroid monkeyrunner">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2016-07-25T13:35:57.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android测试总结">
<meta name="twitter:description" content="Android测试总结 [TOC] 简介最近整理了Android测试方便的只是，主要涉及代码测试和自动化测试。 代码测试JunitJUnit浅谈 MockitoMockito浅谈 MockwebserverMockWebServer Android自动化测试Android monkeyAndroid Monkey整理 Android monkeyrunnerAndroid monkeyrunner">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'GRLHBBCD9E',
      apiKey: '759ab3f5c09b132b3ed4842a44c53c45',
      indexName: 'mycommons',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://mycommons.cn/2016/07/25/Android-Test/"/>





  <title>Android测试总结 | 流水不腐小夏</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?55458ffca1f60dd283f11b9a7f82b262";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">流水不腐小夏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">http://mycommons.cn</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup search-popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://mycommons.cn/2016/07/25/Android-Test/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="流水不腐小夏">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="流水不腐小夏">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Android测试总结</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-07-25T21:35:18+08:00">
                2016-07-25
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016/07/25/Android-Test/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2016/07/25/Android-Test/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Android测试总结"><a href="#Android测试总结" class="headerlink" title="Android测试总结"></a>Android测试总结</h1><hr>
<p>[TOC]</p>
<h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>最近整理了Android测试方便的只是，主要涉及代码测试和自动化测试。</p>
<h1 id="代码测试"><a href="#代码测试" class="headerlink" title="代码测试"></a>代码测试</h1><h2 id="Junit"><a href="#Junit" class="headerlink" title="Junit"></a>Junit</h2><p><a href="http://mycommons.cn/2016/07/19/JUnit%E6%B5%85%E8%B0%88/">JUnit浅谈</a></p>
<h2 id="Mockito"><a href="#Mockito" class="headerlink" title="Mockito"></a>Mockito</h2><p><a href="http://mycommons.cn/2016/07/19/Mockito%E6%B5%85%E8%B0%88/">Mockito浅谈</a></p>
<h2 id="Mockwebserver"><a href="#Mockwebserver" class="headerlink" title="Mockwebserver"></a>Mockwebserver</h2><p><a href="http://mycommons.cn/2016/07/25/MockWebServer/">MockWebServer</a></p>
<h1 id="Android自动化测试"><a href="#Android自动化测试" class="headerlink" title="Android自动化测试"></a>Android自动化测试</h1><h2 id="Android-monkey"><a href="#Android-monkey" class="headerlink" title="Android monkey"></a>Android monkey</h2><p><a href="http://mycommons.cn/2016/07/19/Android-Monkey%E6%95%B4%E7%90%86/">Android Monkey整理</a></p>
<h2 id="Android-monkeyrunner"><a href="#Android-monkeyrunner" class="headerlink" title="Android monkeyrunner"></a>Android monkeyrunner</h2><p><a href="http://mycommons.cn/2016/07/25/AndroidMonkeyRunner/">Android monkeyrunner整理</a></p>
<h2 id="Android-UIAutomator"><a href="#Android-UIAutomator" class="headerlink" title="Android UIAutomator"></a>Android UIAutomator</h2><p><a href="http://mycommons.cn/2016/07/25/Android-UIAutomator/">Android UIAutomator浅谈</a></p>
<h2 id="Android-Espresso"><a href="#Android-Espresso" class="headerlink" title="Android Espresso"></a>Android Espresso</h2><p><a href="http://mycommons.cn/2016/07/25/Android-Espresso/">Android Espresso浅谈</a></p>
<h2 id="自动化测试示例"><a href="#自动化测试示例" class="headerlink" title="自动化测试示例"></a>自动化测试示例</h2><p>下面示例一个Android项目，就是一个简单的登录页面，依次使用上面介绍的自动化测试方案测试界面。</p>
<p>首先是界面布局:<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="utf-8"?&gt;</div><div class="line"><span class="tag">&lt;<span class="name">LinearLayout</span> <span class="attr">android:id</span>=<span class="string">"@+id/activity_main"</span></span></div><div class="line"><span class="tag">    <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></div><div class="line"><span class="tag">    <span class="attr">xmlns:tools</span>=<span class="string">"http://schemas.android.com/tools"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:orientation</span>=<span class="string">"vertical"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:padding</span>=<span class="string">"20dp"</span></span></div><div class="line"><span class="tag">    <span class="attr">tools:context</span>=<span class="string">"cn.mycommons.testcase.MainActivity"</span>&gt;</span></div><div class="line"></div><div class="line">    <span class="tag">&lt;<span class="name">EditText</span></span></div><div class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/edtUserName"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"50dp"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:hint</span>=<span class="string">"Input user name"</span> /&gt;</span></div><div class="line"></div><div class="line">    <span class="tag">&lt;<span class="name">EditText</span></span></div><div class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/edtPasswd"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"50dp"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:hint</span>=<span class="string">"Input password"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:contentDescription</span>=<span class="string">"Input password"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:inputType</span>=<span class="string">"textPassword"</span> /&gt;</span></div><div class="line"></div><div class="line">    <span class="tag">&lt;<span class="name">Button</span></span></div><div class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/btnLogin"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"50dp"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:text</span>=<span class="string">"Login"</span> /&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></div></pre></td></tr></table></figure></p>
<p>其次是页面代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> cn.mycommons.testcase;</div><div class="line"></div><div class="line"><span class="keyword">import</span> android.content.Intent;</div><div class="line"><span class="keyword">import</span> android.os.Bundle;</div><div class="line"><span class="keyword">import</span> android.support.v7.app.AppCompatActivity;</div><div class="line"><span class="keyword">import</span> android.view.View;</div><div class="line"><span class="keyword">import</span> android.widget.Button;</div><div class="line"><span class="keyword">import</span> android.widget.EditText;</div><div class="line"><span class="keyword">import</span> android.widget.Toast;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> </span>&#123;</div><div class="line"></div><div class="line">    EditText edtUserName;</div><div class="line">    EditText edtPasswd;</div><div class="line">    Button btnLogin;</div><div class="line"></div><div class="line">    String userName;</div><div class="line">    String passwd;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</div><div class="line">        setContentView(R.layout.activity_main);</div><div class="line"></div><div class="line">        edtUserName = (EditText) findViewById(R.id.edtUserName);</div><div class="line">        edtPasswd = (EditText) findViewById(R.id.edtPasswd);</div><div class="line">        btnLogin = (Button) findViewById(R.id.btnLogin);</div><div class="line"></div><div class="line">        btnLogin.setOnClickListener(<span class="keyword">new</span> View.OnClickListener() &#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View view)</span> </span>&#123;</div><div class="line">                userName = edtUserName.getText().toString();</div><div class="line">                passwd = edtPasswd.getText().toString();</div><div class="line">                <span class="keyword">if</span> (check(userName, passwd)) &#123;</div><div class="line">                    doLogin(userName, passwd);</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">        &#125;);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">boolean</span> <span class="title">check</span><span class="params">(String userName, String passwd)</span> </span>&#123;</div><div class="line">        <span class="keyword">do</span> &#123;</div><div class="line">            <span class="keyword">if</span> (userName.length() &lt; <span class="number">5</span>) &#123;</div><div class="line">                showToast(<span class="string">"User name invalidate"</span>);</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">if</span> (passwd.length() &lt; <span class="number">5</span>) &#123;</div><div class="line">                showToast(<span class="string">"Password invalidate"</span>);</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">            &#125;</div><div class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line">        &#125; <span class="keyword">while</span> (<span class="keyword">false</span>);</div><div class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">doLogin</span><span class="params">(String userName, String passwd)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (<span class="string">"admin"</span>.equals(userName) &amp;&amp; <span class="string">"admin"</span>.equals(passwd)) &#123;</div><div class="line">            showToast(<span class="string">"Login success"</span>);</div><div class="line">            startActivity(<span class="keyword">new</span> Intent(<span class="keyword">this</span>, SuccessActivity.class));</div><div class="line">        &#125; <span class="keyword">else</span> &#123;</div><div class="line">            showToast(<span class="string">"Login fail"</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">showToast</span><span class="params">(String msg)</span> </span>&#123;</div><div class="line">        Toast.makeText(<span class="keyword">this</span>, msg, Toast.LENGTH_SHORT).show();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="Monkey"><a href="#Monkey" class="headerlink" title="Monkey"></a>Monkey</h3><p>Monkey只是检查使用下shell命令即可。<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">adb shell monkey -p cn.mycommons.testcase -v 50000</div></pre></td></tr></table></figure></p>
<h3 id="Monkey-Runner"><a href="#Monkey-Runner" class="headerlink" title="Monkey Runner"></a>Monkey Runner</h3><p>Monkey Runner提供的是一个python文件，然后调用monkeyrunner命令即可。<br><figure class="highlight sh"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">$ monkeyrunner test_case.py</div></pre></td></tr></table></figure></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># Imports the monkeyrunner modules used by this program</span></div><div class="line"><span class="keyword">from</span> com.android.monkeyrunner <span class="keyword">import</span> MonkeyRunner, MonkeyDevice</div><div class="line"></div><div class="line"><span class="comment"># Connects to the current device, returning a MonkeyDevice object</span></div><div class="line"><span class="keyword">print</span> <span class="string">'wait for device connection.'</span></div><div class="line">device = MonkeyRunner.waitForConnection()</div><div class="line"><span class="keyword">print</span> <span class="string">'connect device success.'</span></div><div class="line"></div><div class="line"><span class="comment"># Takes a screenshot</span></div><div class="line">result = device.takeSnapshot()</div><div class="line"><span class="keyword">print</span> <span class="string">'takeSnapshot success.'</span></div><div class="line"><span class="comment"># Writes the screenshot to a file</span></div><div class="line">result.writeToFile(<span class="string">'./test_case1.png'</span>,<span class="string">'png'</span>)</div><div class="line"><span class="keyword">print</span> <span class="string">'save image to file success.'</span></div><div class="line"></div><div class="line"><span class="comment"># input user name</span></div><div class="line">device.touch(<span class="number">200</span>,<span class="number">380</span>,<span class="string">'DOWN_AND_UP'</span>)</div><div class="line"><span class="keyword">print</span> <span class="string">'touch user name'</span></div><div class="line"><span class="keyword">for</span> x <span class="keyword">in</span> xrange(<span class="number">1</span>,<span class="number">100</span>):</div><div class="line">	device.press(<span class="string">"KEYCODE_DEL"</span>,<span class="string">'DOWN_AND_UP'</span>)</div><div class="line"><span class="keyword">print</span> <span class="string">'delete user name'</span></div><div class="line">device.type(<span class="string">"admin"</span>)</div><div class="line"><span class="keyword">print</span> <span class="string">'input admin to user name'</span></div><div class="line"></div><div class="line"><span class="comment"># input passwd </span></div><div class="line">device.touch(<span class="number">200</span>,<span class="number">500</span>,<span class="string">'DOWN_AND_UP'</span>)</div><div class="line"><span class="keyword">print</span> <span class="string">'touch password'</span></div><div class="line"><span class="keyword">for</span> x <span class="keyword">in</span> xrange(<span class="number">1</span>,<span class="number">100</span>):</div><div class="line">	device.press(<span class="string">"KEYCODE_DEL"</span>,<span class="string">'DOWN_AND_UP'</span>)</div><div class="line"><span class="keyword">print</span> <span class="string">'delete password'</span></div><div class="line">device.type(<span class="string">"admin"</span>)</div><div class="line"><span class="keyword">print</span> <span class="string">'input admin to password'</span></div><div class="line"></div><div class="line"><span class="comment"># press login button</span></div><div class="line">device.touch(<span class="number">200</span>,<span class="number">680</span>,<span class="string">'DOWN_AND_UP'</span>)</div><div class="line"><span class="keyword">print</span> <span class="string">'press login button'</span></div><div class="line"></div><div class="line">MonkeyRunner.sleep(<span class="number">2</span>)</div><div class="line"></div><div class="line"><span class="comment"># Takes a screenshot</span></div><div class="line">result = device.takeSnapshot()</div><div class="line"><span class="keyword">print</span> <span class="string">'takeSnapshot success.'</span></div><div class="line"><span class="comment"># Writes the screenshot to a file</span></div><div class="line">result.writeToFile(<span class="string">'./test_case2.png'</span>,<span class="string">'png'</span>)</div><div class="line"><span class="keyword">print</span> <span class="string">'save image to file success.'</span></div></pre></td></tr></table></figure>
<h3 id="UiAutomator"><a href="#UiAutomator" class="headerlink" title="UiAutomator"></a>UiAutomator</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> cn.mycommons.testcase;</div><div class="line"></div><div class="line"><span class="keyword">import</span> android.content.Context;</div><div class="line"><span class="keyword">import</span> android.content.Intent;</div><div class="line"><span class="keyword">import</span> android.os.Build;</div><div class="line"><span class="keyword">import</span> android.support.test.InstrumentationRegistry;</div><div class="line"><span class="keyword">import</span> android.support.test.filters.SdkSuppress;</div><div class="line"><span class="keyword">import</span> android.support.test.runner.AndroidJUnit4;</div><div class="line"><span class="keyword">import</span> android.support.test.uiautomator.By;</div><div class="line"><span class="keyword">import</span> android.support.test.uiautomator.UiDevice;</div><div class="line"><span class="keyword">import</span> android.support.test.uiautomator.UiObject;</div><div class="line"><span class="keyword">import</span> android.support.test.uiautomator.UiObjectNotFoundException;</div><div class="line"><span class="keyword">import</span> android.support.test.uiautomator.UiSelector;</div><div class="line"><span class="keyword">import</span> android.support.test.uiautomator.Until;</div><div class="line"><span class="keyword">import</span> org.hamcrest.Matchers;</div><div class="line"><span class="keyword">import</span> org.junit.Before;</div><div class="line"><span class="keyword">import</span> org.junit.Test;</div><div class="line"><span class="keyword">import</span> org.junit.runner.RunWith;</div><div class="line"><span class="keyword">import</span> <span class="keyword">static</span> org.junit.Assert.assertThat;</div><div class="line"></div><div class="line"><span class="meta">@RunWith</span>(AndroidJUnit4.class)</div><div class="line"><span class="meta">@SdkSuppress</span>(minSdkVersion = Build.VERSION_CODES.JELLY_BEAN_MR2)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivityTest</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String BASIC_SAMPLE_PACKAGE = <span class="string">"cn.mycommons.testcase"</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> LAUNCH_TIMEOUT = <span class="number">5000</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String STRING_TO_BE_TYPED = <span class="string">"UiAutomator"</span>;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> UiDevice mDevice;</div><div class="line"></div><div class="line">    <span class="meta">@Before</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">before</span><span class="params">()</span> </span>&#123;</div><div class="line">        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());</div><div class="line">        mDevice.pressHome();</div><div class="line"></div><div class="line">        <span class="comment">// Wait for launcher</span></div><div class="line">        <span class="keyword">final</span> String launcherPackage = mDevice.getLauncherPackageName();</div><div class="line">        assertThat(launcherPackage, Matchers.notNullValue());</div><div class="line">        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(<span class="number">0</span>)), LAUNCH_TIMEOUT);</div><div class="line"></div><div class="line">        Context context = InstrumentationRegistry.getContext();</div><div class="line">        <span class="keyword">final</span> Intent intent = context.getPackageManager().getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);</div><div class="line">        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);</div><div class="line">        context.startActivity(intent);</div><div class="line"></div><div class="line">        <span class="comment">// Wait for the app to appear</span></div><div class="line">        mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(<span class="number">0</span>)), LAUNCH_TIMEOUT);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin1</span><span class="params">()</span> <span class="keyword">throws</span> UiObjectNotFoundException </span>&#123;</div><div class="line">        UiObject login = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Login"</span>));</div><div class="line">        UiObject edtUserName = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Input user name"</span>));</div><div class="line">        UiObject edtPasswd = mDevice.findObject(<span class="keyword">new</span> UiSelector().descriptionContains(<span class="string">"Input password"</span>));</div><div class="line"></div><div class="line">        edtUserName.clearTextField();</div><div class="line">        edtPasswd.clearTextField();</div><div class="line"></div><div class="line">        edtUserName.setText(<span class="string">"admin"</span>);</div><div class="line">        edtPasswd.setText(<span class="string">"admin"</span>);</div><div class="line">        login.click();</div><div class="line"></div><div class="line">        mDevice.waitForWindowUpdate(BuildConfig.FLAVOR, <span class="number">3000</span>);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin2</span><span class="params">()</span> <span class="keyword">throws</span> UiObjectNotFoundException </span>&#123;</div><div class="line">        UiObject login = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Login"</span>));</div><div class="line">        UiObject edtUserName = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Input user name"</span>));</div><div class="line">        UiObject edtPasswd = mDevice.findObject(<span class="keyword">new</span> UiSelector().descriptionContains(<span class="string">"Input password"</span>));</div><div class="line"></div><div class="line">        edtUserName.clearTextField();</div><div class="line">        edtPasswd.clearTextField();</div><div class="line">        edtUserName.setText(<span class="string">""</span>);</div><div class="line">        edtPasswd.setText(<span class="string">""</span>);</div><div class="line"></div><div class="line">        login.click();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin3</span><span class="params">()</span> <span class="keyword">throws</span> UiObjectNotFoundException </span>&#123;</div><div class="line">        UiObject login = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Login"</span>));</div><div class="line">        UiObject edtUserName = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Input user name"</span>));</div><div class="line">        UiObject edtPasswd = mDevice.findObject(<span class="keyword">new</span> UiSelector().descriptionContains(<span class="string">"Input password"</span>));</div><div class="line"></div><div class="line">        edtUserName.clearTextField();</div><div class="line">        edtPasswd.clearTextField();</div><div class="line">        edtUserName.setText(<span class="string">"abc"</span>);</div><div class="line">        edtPasswd.setText(<span class="string">""</span>);</div><div class="line"></div><div class="line">        login.click();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin4</span><span class="params">()</span> <span class="keyword">throws</span> UiObjectNotFoundException </span>&#123;</div><div class="line">        UiObject login = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Login"</span>));</div><div class="line">        UiObject edtUserName = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Input user name"</span>));</div><div class="line">        UiObject edtPasswd = mDevice.findObject(<span class="keyword">new</span> UiSelector().descriptionContains(<span class="string">"Input password"</span>));</div><div class="line"></div><div class="line">        edtUserName.clearTextField();</div><div class="line">        edtPasswd.clearTextField();</div><div class="line">        edtUserName.setText(<span class="string">""</span>);</div><div class="line">        edtPasswd.setText(<span class="string">"abc"</span>);</div><div class="line"></div><div class="line">        login.click();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin5</span><span class="params">()</span> <span class="keyword">throws</span> UiObjectNotFoundException </span>&#123;</div><div class="line">        UiObject login = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Login"</span>));</div><div class="line">        UiObject edtUserName = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Input user name"</span>));</div><div class="line">        UiObject edtPasswd = mDevice.findObject(<span class="keyword">new</span> UiSelector().descriptionContains(<span class="string">"Input password"</span>));</div><div class="line"></div><div class="line">        edtUserName.clearTextField();</div><div class="line">        edtPasswd.clearTextField();</div><div class="line">        edtUserName.setText(<span class="string">"abc"</span>);</div><div class="line">        edtPasswd.setText(<span class="string">"abc"</span>);</div><div class="line"></div><div class="line">        login.click();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin6</span><span class="params">()</span> <span class="keyword">throws</span> UiObjectNotFoundException </span>&#123;</div><div class="line">        UiObject login = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Login"</span>));</div><div class="line">        UiObject edtUserName = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Input user name"</span>));</div><div class="line">        UiObject edtPasswd = mDevice.findObject(<span class="keyword">new</span> UiSelector().descriptionContains(<span class="string">"Input password"</span>));</div><div class="line"></div><div class="line">        edtUserName.clearTextField();</div><div class="line">        edtPasswd.clearTextField();</div><div class="line">        edtUserName.setText(<span class="string">"abcedf"</span>);</div><div class="line">        edtPasswd.setText(<span class="string">"abc"</span>);</div><div class="line"></div><div class="line">        login.click();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin7</span><span class="params">()</span> <span class="keyword">throws</span> UiObjectNotFoundException </span>&#123;</div><div class="line">        UiObject login = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Login"</span>));</div><div class="line">        UiObject edtUserName = mDevice.findObject(<span class="keyword">new</span> UiSelector().text(<span class="string">"Input user name"</span>));</div><div class="line">        UiObject edtPasswd = mDevice.findObject(<span class="keyword">new</span> UiSelector().descriptionContains(<span class="string">"Input password"</span>));</div><div class="line"></div><div class="line"></div><div class="line">        edtUserName.clearTextField();</div><div class="line">        edtPasswd.clearTextField();</div><div class="line">        edtUserName.setText(<span class="string">"abcedf"</span>);</div><div class="line">        edtPasswd.setText(<span class="string">"abcedf"</span>);</div><div class="line"></div><div class="line">        login.click();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="Espressor"><a href="#Espressor" class="headerlink" title="Espressor"></a>Espressor</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">package</span> cn.mycommons.testcase;</div><div class="line"></div><div class="line"><span class="keyword">import</span> android.support.test.espresso.Espresso;</div><div class="line"><span class="keyword">import</span> android.support.test.espresso.action.ViewActions;</div><div class="line"><span class="keyword">import</span> android.support.test.espresso.matcher.ViewMatchers;</div><div class="line"><span class="keyword">import</span> android.support.test.rule.ActivityTestRule;</div><div class="line"><span class="keyword">import</span> android.support.test.runner.AndroidJUnit4;</div><div class="line"><span class="keyword">import</span> org.junit.Rule;</div><div class="line"><span class="keyword">import</span> org.junit.Test;</div><div class="line"><span class="keyword">import</span> org.junit.runner.RunWith;</div><div class="line"></div><div class="line"><span class="meta">@RunWith</span>(AndroidJUnit4.class)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivityEspressoTest</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Rule</span></div><div class="line">    <span class="keyword">public</span> ActivityTestRule&lt;MainActivity&gt; testRule = <span class="keyword">new</span> ActivityTestRule&lt;&gt;(MainActivity.class);</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin</span><span class="params">()</span> </span>&#123;</div><div class="line">        Espresso.onView(ViewMatchers.withContentDescription(R.id.edtUserName)).perform(ViewActions.typeText(<span class="string">"admin"</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtPasswd)).perform(ViewActions.typeText(<span class="string">"admin"</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.btnLogin)).perform(ViewActions.click());</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin1</span><span class="params">()</span> </span>&#123;</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtUserName)).perform(ViewActions.typeText(<span class="string">""</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtPasswd)).perform(ViewActions.typeText(<span class="string">""</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.btnLogin)).perform(ViewActions.click());</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin2</span><span class="params">()</span> </span>&#123;</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtUserName)).perform(ViewActions.typeText(<span class="string">"abc"</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtPasswd)).perform(ViewActions.typeText(<span class="string">""</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.btnLogin)).perform(ViewActions.click());</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin3</span><span class="params">()</span> </span>&#123;</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtUserName)).perform(ViewActions.typeText(<span class="string">""</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtPasswd)).perform(ViewActions.typeText(<span class="string">"abc"</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.btnLogin)).perform(ViewActions.click());</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin4</span><span class="params">()</span> </span>&#123;</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtUserName)).perform(ViewActions.typeText(<span class="string">"abc"</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtPasswd)).perform(ViewActions.typeText(<span class="string">"abc"</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.btnLogin)).perform(ViewActions.click());</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testLogin5</span><span class="params">()</span> </span>&#123;</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtUserName)).perform(ViewActions.typeText(<span class="string">"abcdef"</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.edtPasswd)).perform(ViewActions.typeText(<span class="string">"abcdef"</span>));</div><div class="line">        Espresso.onView(ViewMatchers.withId(R.id.btnLogin)).perform(ViewActions.click());</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="自动化测试总结"><a href="#自动化测试总结" class="headerlink" title="自动化测试总结"></a>自动化测试总结</h2><ul>
<li><p>Monkey<br>准确来说，这不算是自动化测试，因为其只能产生随机的事件，无法按照既定的步骤操作；</p>
</li>
<li><p>Monkeyrunner<br>优点：操作最为简单，可以录制测试脚本，可视化操作；<br>缺点：主要生成坐标的自动化操作，移植性不强，功能最为局限，上面代码中已经显示出来，完全使用的数字坐标，移植到另外一个设备，则不能运行。</p>
</li>
<li><p>UiAutomator<br>优点：可以对所有操作进行自动化，操作简单；<br>缺点：Android版本需要高于4.3，无法根据控件ID操作，相对来说功能较为局限，但也够用了；</p>
</li>
<li><p>Espresso<br>优点：主要针对某一个APK进行自动化测试，APK可以有源码，也可以没有源码，功能强大；<br>缺点：针对APK操作，因此操作相对复杂；</p>
</li>
</ul>
<p><strong>总结：</strong>由上面介绍可以有这样的结论：测试某个APK，可以选择Espresso；测试过程可能涉及多个APK，选择UiAutomator；一些简单的测试，选择Monkeyrunner；</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat_pay.jpeg" alt="流水不腐小夏 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpeg" alt="流水不腐小夏 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Android测试/" rel="tag"># Android测试</a>
          
            <a href="/tags/测试/" rel="tag"># 测试</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/07/25/MockWebServer/" rel="next" title="MockWebServer">
                <i class="fa fa-chevron-left"></i> MockWebServer
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/12/25/Android-Net/" rel="prev" title="Android Http网络开发神兵利器">
                Android Http网络开发神兵利器 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpeg"
                alt="流水不腐小夏" />
            
              <p class="site-author-name" itemprop="name">流水不腐小夏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/LiushuiXiaoxia" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/58771f980bac" target="_blank" title="简书">
                    
                      <i class="fa fa-fw fa-简书"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://blog.csdn.net/guijiaoba" target="_blank" title="CSDN">
                    
                      <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Android测试总结"><span class="nav-number">1.</span> <span class="nav-text">Android测试总结</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#简介"><span class="nav-number">2.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#代码测试"><span class="nav-number">3.</span> <span class="nav-text">代码测试</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Junit"><span class="nav-number">3.1.</span> <span class="nav-text">Junit</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Mockito"><span class="nav-number">3.2.</span> <span class="nav-text">Mockito</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Mockwebserver"><span class="nav-number">3.3.</span> <span class="nav-text">Mockwebserver</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Android自动化测试"><span class="nav-number">4.</span> <span class="nav-text">Android自动化测试</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Android-monkey"><span class="nav-number">4.1.</span> <span class="nav-text">Android monkey</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Android-monkeyrunner"><span class="nav-number">4.2.</span> <span class="nav-text">Android monkeyrunner</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Android-UIAutomator"><span class="nav-number">4.3.</span> <span class="nav-text">Android UIAutomator</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Android-Espresso"><span class="nav-number">4.4.</span> <span class="nav-text">Android Espresso</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#自动化测试示例"><span class="nav-number">4.5.</span> <span class="nav-text">自动化测试示例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Monkey"><span class="nav-number">4.5.1.</span> <span class="nav-text">Monkey</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Monkey-Runner"><span class="nav-number">4.5.2.</span> <span class="nav-text">Monkey Runner</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#UiAutomator"><span class="nav-number">4.5.3.</span> <span class="nav-text">UiAutomator</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Espressor"><span class="nav-number">4.5.4.</span> <span class="nav-text">Espressor</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#自动化测试总结"><span class="nav-number">4.6.</span> <span class="nav-text">自动化测试总结</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">流水不腐小夏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://mycommons-cn.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://mycommons.cn/2016/07/25/Android-Test/';
          this.page.identifier = '2016/07/25/Android-Test/';
          this.page.title = 'Android测试总结';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://mycommons-cn.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  




  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.3"></script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
